function A(msg) {
	//
	// Create a new div, set the contents with the message, and pop it at the end of the chain... scroll appropriately.
	//
	var bodyDoc = window.top.frames[1].document;
	var newDiv = bodyDoc.createElement("div");
	newDiv.innerHTML = msg;
	bodyDoc.body.appendChild(newDiv);

	if (window.top.frames[4].scrollPolicy == "auto") {
		window.top.frames[1].scrollTo(0, 999999999);
	}
}

function S(url) {
	if (window.top.frames[4].mutePolicy != "muted") {
		var bodyDoc = window.top.frames[1].document;
		var newDiv = bodyDoc.createElement("div");
		newDiv.innerHTML = "<embed autostart=true loop=1 playcount=1 src="+url+" hidden=true>";
		bodyDoc.body.appendChild(newDiv);
	}
}

function clearChat() {
	var bodyDoc = window.top.frames[1].document.body;
	var numNodes = bodyDoc.childNodes.length;
	var thisNode;

	//
	// Old Method:
	//
	//window.top.frames[1].document.body.innerHTML='<b>Chat Buffer cleared.</b><br />';
	
	//
	// New Method (only removes child divs, leaves body.innerHTML mods alone....
	//
	for (thisNode = numNodes - 1; thisNode >= 0; thisNode--) {
		var myNode = bodyDoc.childNodes[thisNode];

		if (myNode.tagName == "DIV") {
			bodyDoc.removeChild(bodyDoc.childNodes[thisNode]);
		}
	}

	var newDiv = window.top.frames[1].document.createElement("div");
	newDiv.innerHTML = "<B>*** Chat buffer cleared.</b><br />";
	bodyDoc.appendChild(newDiv);
}

function changeTopic(msg) {
	window.top.frames[0].document.body.innerHTML=msg;
}

function STC(msg, reload) {
	if (reload == 4) {
		S(msg);
		setFocus();
	} else {
		A(msg);

		if (reload == 1) {
			window.top.frames[0].location.reload();
		} else if (reload == 2) {
			window.top.frames[2].location.reload();
		} else if (reload == 3) {
			window.top.frames[0].location.reload();
			window.top.frames[2].location.reload();
		}
	}
}

function SendToChat(msg) {
	//
	// Compatibility layer... clarity of function name is nice for debugging but longterm the less
	// bytes transferred, the more optimal the system...
	//
	A(msg);
}

function addCSS(cssToAdd) {
	var bodyDoc = window.top.frames[1].document.body;
	bodyDoc.innerHTML	+= cssToAdd;
}

function toggleScrolling() {
	//
	// Pause / unpause scrolling...
	//
	var mainFrame = window.top.frames[4];
	var thisButton = document.forms.msg.pause;
	//alert(mainFrame.scrollPolicy);
	if (mainFrame.scrollPolicy == "auto") {
		mainFrame.scrollPolicy = "paused";
		thisButton.value = "Unpause";
	} else {
		mainFrame.scrollPolicy = "auto";
		thisButton.value = "Pause";
	}
	//alert(mainFrame.scrollPolicy);
}

function toggleMute() {
	//
	// Mute / unmute sounds...
	//
	var mainFrame = window.top.frames[4];
	var thisButton = document.forms.msg.mute;
	//alert(mainFrame.mutePolicy);
	if (mainFrame.mutePolicy == "full") {
		mainFrame.mutePolicy = "muted";
		thisButton.value = "Unmute";
	} else {
		mainFrame.mutePolicy = "full";
		thisButton.value = "Mute";
	}
	//alert(mainFrame.mutePolicy);
}

function setButtons() {
	//
	// Set the name of the buttons appropriately after the user
	// sends a message to chat.
	//
	var mainFrame = window.top.frames[4];
	var muteButton = document.forms.msg.mute;
	var pauseButton = document.forms.msg.pause;
	if (mainFrame.mutePolicy == "full") {
		muteButton.value = "Mute";
	} else {
		muteButton.value = "Unmute";
	}
	if (mainFrame.scrollPolicy == "auto") {
		pauseButton.value = "Pause";
	} else {
		pauseButton.value = "Unpause";
	}
}

function setMID(mid) {
	var mainFrame = window.top.frames[4];
	var midDiv = mainFrame.document.getElementById('midDiv');

	mainFrame.mid = mid;
	midDiv.innerHTML = "MID is <b>" + mid + "</b>";
}

function setURL(url) {
	var mainFrame = window.top.frames[4];
	var urlDiv = mainFrame.document.getElementById('urlDiv');

	mainFrame.baseURL = url;
	urlDiv.innerHTML = "Base URL is <b>" + url + "</b>";
}

function getNewEvents() {
	var pullFrame = window.top.frames[5];
	var mainFrame = window.top.frames[4];

	var pullURL = mainFrame.baseURL + '&mid=' + mainFrame.mid + '&cpsp=1';
	var errURL = '&mid=' + mainFrame.mid + '&cpsp=1';

	if (errURL != pullURL) {
		//alert("Loading: " + pullURL);
		setTimeout("getNewEvents();", 5000);
		pullFrame.document.location = pullURL;
	}
}

function setFocus() {
	document.msg.message.focus();
}

function smilie(smile) {
	document.msg.message.value += ' ' + smile + ' ';
	document.msg.message.focus();
}
